package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressAddNewParam;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressStandardVO;
import cn.tedu.tmall.front.mall.service.IReceiverAddressService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/receiver-addresses")
@Validated
@Api(tags = "04. 收货地址管理")
public class ReceiverAddressController {

    @Autowired
    private IReceiverAddressService receiverAddressService;

    @PostMapping("/add-new")
    @ApiOperation("新增收货地址")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @Valid ReceiverAddressAddNewParam receiverAddressAddNewParam) {
        receiverAddressService.addNew(currentPrincipal,receiverAddressAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询收货地址")
    @ApiOperationSupport(order = 410)
    public JsonResult getStandardById(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                      @PathVariable @Range(min = 1, message = "请提交有效的收货地址ID值!") Long id) {
        ReceiverAddressStandardVO queryResult = receiverAddressService.getStandardById(currentPrincipal, id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("查询收货地址列表")
    @ApiOperationSupport(order = 421)
    public JsonResult listByUser(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        List<ReceiverAddressListItemVO> list = receiverAddressService.listByUser(currentPrincipal);
        return JsonResult.ok(list);
    }
}
